import { request } from '@/app/http'
import { toPageParam, toPageResult, toRangeParam } from 'app/converter'

const PATH = '/dict'

const ITEM_PATH = '/dictDetail'

export async function allDict() {
  const { data } = await request({
    url: PATH + '/all',
    method: 'get'
  })
  return data
}

export async function searchDict(params, pageData) {
  const { data } = await request({
    url: PATH,
    method: 'get',
    params: { ...toRangeParam(params), ...toPageParam(pageData) }
  })
  return toPageResult(data)
}

export function addDict(data) {
  return request({
    url: PATH,
    method: 'post',
    data
  })
}

export function updateDict(data) {
  return request({
    url: PATH,
    method: 'put',
    data
  })
}

export function removeDict(data) {
  return request({
    url: PATH,
    method: 'delete',
    data,
    showLoading: true,
    showMsg: true
  })
}

export function downloadDict(params) {
  return request({
    url: PATH + '/download',
    method: 'get',
    params,
    responseType: 'blob',
    showLoading: true,
    showMsg: true
  })
}

export async function searchDictItem(params, pageData) {
  const { data } = await request({
    url: ITEM_PATH,
    method: 'get',
    params: { ...toRangeParam(params), ...toPageParam(pageData) }
  })
  return toPageResult(data)
}

export function addDictItem(data) {
  return request({
    url: ITEM_PATH,
    method: 'post',
    data
  })
}

export function updateDictItem(data) {
  return request({
    url: ITEM_PATH,
    method: 'put',
    data
  })
}

export function removeDictItem(data) {
  return request({
    url: ITEM_PATH,
    method: 'delete',
    data,
    showLoading: true,
    showMsg: true
  })
}

export function mapDict(params) {
  return request({
    url: ITEM_PATH + '/map',
    method: 'get',
    params
  })
}
